package com.nuc.kl.module.usermanager.action;


import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.usermanager.entity.Student;
import com.nuc.kl.module.usermanager.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
public class RegisterAction extends ActionSupport{
	
	private String findName;
	
	private String loginName;
	private String password;
	
	
	@Resource
	private StudentService studentService;
	
	public String findByName() throws Exception{
		
		List<Student> students = studentService.findByName(findName); 
		if(students.isEmpty()){
			sendMessage("ok", "可以使用！");
		}else{
			sendMessage("error", "已被注册！");
		}
		return null;
		
	}
	
	public String  register() throws Exception{
		
		Student student = new Student();
		student.setLoginName(loginName);
		String md5Digest = DigestUtils.md5Hex(password);
		student.setPassword(md5Digest);
		
		studentService.save(student);
		
		return SUCCESS;
	}
	
	public void sendMessage(String status,String msg) throws IOException{
		//输出提示语句
			String str = "{\"status\":\""+status+"\" , \"message\":\""+msg+"!\"}";
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");//指定为utf-8  
			response.getWriter().write(str);
	}
	
	
//	----------------------------------------------------
	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
}
